<template>
  <div class="dashboard-container">
    <!-- 页面标题 -->
    <div class="page-header">
      <h2>调控业务数据安全管控及服务优化平台</h2>
      <p class="welcome-text">欢迎使用，{{ currentUser }}！</p>
    </div>

    <!-- 数据统计卡片 -->
    <el-row :gutter="20" class="stats-row">
      <el-col :xs="24" :sm="12" :lg="6">
        <div class="stat-card stat-card-primary">
          <div class="stat-icon">
            <i class="el-icon-document"></i>
          </div>
          <div class="stat-content">
            <div class="stat-value">{{ statisticsData.totalData }}</div>
            <div class="stat-label">数据总量</div>
            <div class="stat-trend">
              <span class="trend-up">
                <i class="el-icon-top"></i> {{ statisticsData.dataTrend }}%
              </span>
            </div>
          </div>
        </div>
      </el-col>
      
      <el-col :xs="24" :sm="12" :lg="6">
        <div class="stat-card stat-card-success">
          <div class="stat-icon">
            <i class="el-icon-success"></i>
          </div>
          <div class="stat-content">
            <div class="stat-value">{{ statisticsData.safeCount }}</div>
            <div class="stat-label">安全事件处理</div>
            <div class="stat-trend">
              <span class="trend-up">
                <i class="el-icon-top"></i> {{ statisticsData.safeTrend }}%
              </span>
            </div>
          </div>
        </div>
      </el-col>
      
      <el-col :xs="24" :sm="12" :lg="6">
        <div class="stat-card stat-card-warning">
          <div class="stat-icon">
            <i class="el-icon-warning"></i>
          </div>
          <div class="stat-content">
            <div class="stat-value">{{ statisticsData.warningCount }}</div>
            <div class="stat-label">待处理告警</div>
            <div class="stat-trend">
              <span class="trend-down">
                <i class="el-icon-bottom"></i> {{ statisticsData.warningTrend }}%
              </span>
            </div>
          </div>
        </div>
      </el-col>
      
      <el-col :xs="24" :sm="12" :lg="6">
        <div class="stat-card stat-card-info">
          <div class="stat-icon">
            <i class="el-icon-user"></i>
          </div>
          <div class="stat-content">
            <div class="stat-value">{{ statisticsData.onlineUsers }}</div>
            <div class="stat-label">在线用户</div>
            <div class="stat-trend">
              <span class="trend-normal">
                <i class="el-icon-minus"></i> {{ statisticsData.userTrend }}%
              </span>
            </div>
          </div>
        </div>
      </el-col>
    </el-row>

    <!-- 图表区域 -->
    <el-row :gutter="20" class="chart-row">
      <el-col :xs="24" :lg="16">
        <el-card class="chart-card">
          <div slot="header" class="card-header">
            <span>数据安全态势</span>
            <el-radio-group v-model="trendPeriod" size="mini" @change="handlePeriodChange">
              <el-radio-button label="day">今日</el-radio-button>
              <el-radio-button label="week">本周</el-radio-button>
              <el-radio-button label="month">本月</el-radio-button>
            </el-radio-group>
          </div>
          <div ref="trendChart" class="chart-container"></div>
        </el-card>
      </el-col>
      
      <el-col :xs="24" :lg="8">
        <el-card class="chart-card">
          <div slot="header" class="card-header">
            <span>安全事件分布</span>
          </div>
          <div ref="pieChart" class="chart-container-small"></div>
        </el-card>
      </el-col>
    </el-row>

    <!-- 实时监控和快捷操作 -->
    <el-row :gutter="20" class="bottom-row">
      <el-col :xs="24" :lg="12">
        <el-card class="monitor-card">
          <div slot="header" class="card-header">
            <span>实时监控</span>
            <el-button type="text" size="small" @click="refreshMonitor">
              <i class="el-icon-refresh"></i> 刷新
            </el-button>
          </div>
          <el-table :data="monitorData" style="width: 100%" max-height="300">
            <el-table-column prop="time" label="时间" width="160"></el-table-column>
            <el-table-column prop="type" label="类型" width="120">
              <template slot-scope="scope">
                <el-tag :type="getTypeTag(scope.row.type)" size="small">
                  {{ scope.row.type }}
                </el-tag>
              </template>
            </el-table-column>
            <el-table-column prop="content" label="内容" show-overflow-tooltip></el-table-column>
            <el-table-column prop="status" label="状态" width="80">
              <template slot-scope="scope">
                <el-tag :type="scope.row.status === '正常' ? 'success' : 'danger'" size="mini">
                  {{ scope.row.status }}
                </el-tag>
              </template>
            </el-table-column>
          </el-table>
        </el-card>
      </el-col>
      
      <el-col :xs="24" :lg="12">
        <el-card class="quick-action-card">
          <div slot="header" class="card-header">
            <span>快捷操作</span>
          </div>
          <div class="quick-actions">
            <el-row :gutter="15">
              <el-col :span="8" v-for="(action, index) in quickActions" :key="index">
                <div class="action-item" @click="handleQuickAction(action.key)">
                  <div class="action-icon" :class="'action-icon-' + action.color">
                    <i :class="action.icon"></i>
                  </div>
                  <div class="action-label">{{ action.label }}</div>
                </div>
              </el-col>
            </el-row>
          </div>
        </el-card>

        <el-card class="system-info-card" style="margin-top: 20px;">
          <div slot="header" class="card-header">
            <span>系统信息</span>
          </div>
          <div class="system-info">
            <div class="info-item">
              <span class="info-label">系统版本：</span>
              <span class="info-value">{{ version }}</span>
            </div>
            <div class="info-item">
              <span class="info-label">运行状态：</span>
              <span class="info-value">
                <el-tag type="success" size="mini">运行中</el-tag>
              </span>
            </div>
            <div class="info-item">
              <span class="info-label">最后更新：</span>
              <span class="info-value">{{ lastUpdateTime }}</span>
            </div>
          </div>
        </el-card>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import * as echarts from 'echarts'

export default {
  name: "Index",
  data() {
    return {
      version: "3.9.0",
      currentUser: "管理员",
      trendPeriod: "day",
      lastUpdateTime: "",
      statisticsData: {
        totalData: "1,234,567",
        dataTrend: 12.5,
        safeCount: "856",
        safeTrend: 8.3,
        warningCount: "23",
        warningTrend: 15.2,
        onlineUsers: "128",
        userTrend: 2.1
      },
      monitorData: [
        { time: "2025-10-11 14:23:15", type: "数据访问", content: "用户张三访问敏感数据", status: "正常" },
        { time: "2025-10-11 14:20:32", type: "安全告警", content: "检测到异常登录尝试", status: "异常" },
        { time: "2025-10-11 14:18:45", type: "系统操作", content: "数据备份完成", status: "正常" },
        { time: "2025-10-11 14:15:20", type: "数据同步", content: "业务数据同步成功", status: "正常" },
        { time: "2025-10-11 14:12:08", type: "权限变更", content: "用户权限更新", status: "正常" }
      ],
      quickActions: [
        { key: "monitor", label: "实时监控", icon: "el-icon-view", color: "blue" },
        { key: "security", label: "安全审计", icon: "el-icon-lock", color: "green" },
        { key: "data", label: "数据管理", icon: "el-icon-s-data", color: "purple" },
        { key: "user", label: "用户管理", icon: "el-icon-user-solid", color: "orange" },
        { key: "config", label: "系统配置", icon: "el-icon-setting", color: "cyan" },
        { key: "report", label: "统计报表", icon: "el-icon-s-marketing", color: "red" }
      ],
      trendChart: null,
      pieChart: null
    }
  },
  mounted() {
    this.updateTime()
    this.initCharts()
    // 每30秒刷新一次时间
    setInterval(this.updateTime, 30000)
  },
  beforeDestroy() {
    if (this.trendChart) {
      this.trendChart.dispose()
    }
    if (this.pieChart) {
      this.pieChart.dispose()
    }
  },
  methods: {
    updateTime() {
      const now = new Date()
      this.lastUpdateTime = now.toLocaleString('zh-CN')
    },
    initCharts() {
      this.initTrendChart()
      this.initPieChart()
    },
    initTrendChart() {
      if (this.trendChart) {
        this.trendChart.dispose()
      }
      this.trendChart = echarts.init(this.$refs.trendChart)
      const option = {
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            type: 'cross'
          }
        },
        legend: {
          data: ['数据访问量', '安全事件', '系统告警']
        },
        grid: {
          left: '3%',
          right: '4%',
          bottom: '3%',
          containLabel: true
        },
        xAxis: {
          type: 'category',
          boundaryGap: false,
          data: ['00:00', '04:00', '08:00', '12:00', '16:00', '20:00', '24:00']
        },
        yAxis: {
          type: 'value'
        },
        series: [
          {
            name: '数据访问量',
            type: 'line',
            smooth: true,
            data: [120, 132, 101, 134, 190, 230, 210],
            itemStyle: { color: '#409EFF' }
          },
          {
            name: '安全事件',
            type: 'line',
            smooth: true,
            data: [220, 182, 191, 234, 290, 330, 310],
            itemStyle: { color: '#67C23A' }
          },
          {
            name: '系统告警',
            type: 'line',
            smooth: true,
            data: [15, 23, 20, 15, 19, 33, 28],
            itemStyle: { color: '#E6A23C' }
          }
        ]
      }
      this.trendChart.setOption(option)
    },
    initPieChart() {
      if (this.pieChart) {
        this.pieChart.dispose()
      }
      this.pieChart = echarts.init(this.$refs.pieChart)
      const option = {
        tooltip: {
          trigger: 'item'
        },
        legend: {
          bottom: '5%',
          left: 'center'
        },
        series: [
          {
            name: '安全事件',
            type: 'pie',
            radius: ['40%', '70%'],
            avoidLabelOverlap: false,
            itemStyle: {
              borderRadius: 10,
              borderColor: '#fff',
              borderWidth: 2
            },
            label: {
              show: false,
              position: 'center'
            },
            emphasis: {
              label: {
                show: true,
                fontSize: 20,
                fontWeight: 'bold'
              }
            },
            labelLine: {
              show: false
            },
            data: [
              { value: 335, name: '数据访问', itemStyle: { color: '#409EFF' } },
              { value: 234, name: '权限变更', itemStyle: { color: '#67C23A' } },
              { value: 154, name: '异常操作', itemStyle: { color: '#E6A23C' } },
              { value: 135, name: '登录审计', itemStyle: { color: '#F56C6C' } },
              { value: 85, name: '其他', itemStyle: { color: '#909399' } }
            ]
          }
        ]
      }
      this.pieChart.setOption(option)
    },
    handlePeriodChange() {
      // 根据选择的时间段更新图表数据
      this.initTrendChart()
    },
    refreshMonitor() {
      this.$message.success('数据已刷新')
      // 这里可以添加实际的刷新逻辑
    },
    getTypeTag(type) {
      const typeMap = {
        '数据访问': 'primary',
        '安全告警': 'danger',
        '系统操作': 'success',
        '数据同步': 'info',
        '权限变更': 'warning'
      }
      return typeMap[type] || 'info'
    },
    handleQuickAction(key) {
      this.$message.info(`跳转到${key}功能`)
      // 这里可以添加路由跳转逻辑
    }
  }
}
</script>

<style scoped lang="scss">
.dashboard-container {
  padding: 20px;
  background-color: #f0f2f5;
  min-height: calc(100vh - 84px);
}

.page-header {
  margin-bottom: 30px;
  
  h2 {
    margin: 0 0 5px 0;
    font-size: 28px;
    font-weight: 500;
    color: #303133;
  }
  
  .welcome-text {
    margin: 0;
    color: #909399;
    font-size: 14px;
  }
}

.stats-row {
  margin-bottom: 20px;
}

.stat-card {
  background: #fff;
  border-radius: 8px;
  padding: 20px;
  display: flex;
  align-items: center;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.05);
  transition: all 0.3s;
  cursor: pointer;
  margin-bottom: 20px;
  
  &:hover {
    transform: translateY(-3px);
    box-shadow: 0 4px 20px 0 rgba(0, 0, 0, 0.1);
  }
  
  .stat-icon {
    width: 60px;
    height: 60px;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 28px;
    margin-right: 20px;
  }
  
  .stat-content {
    flex: 1;
    
    .stat-value {
      font-size: 28px;
      font-weight: bold;
      margin-bottom: 5px;
    }
    
    .stat-label {
      color: #909399;
      font-size: 14px;
      margin-bottom: 8px;
    }
    
    .stat-trend {
      font-size: 12px;
      
      .trend-up {
        color: #67C23A;
      }
      
      .trend-down {
        color: #F56C6C;
      }
      
      .trend-normal {
        color: #909399;
      }
    }
  }
  
  &.stat-card-primary {
    .stat-icon {
      background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
      color: #fff;
    }
    .stat-value {
      color: #667eea;
    }
  }
  
  &.stat-card-success {
    .stat-icon {
      background: linear-gradient(135deg, #5ee7df 0%, #b490ca 100%);
      color: #fff;
    }
    .stat-value {
      color: #5ee7df;
    }
  }
  
  &.stat-card-warning {
    .stat-icon {
      background: linear-gradient(135deg, #fad0c4 0%, #ffd1ff 100%);
      color: #E6A23C;
    }
    .stat-value {
      color: #E6A23C;
    }
  }
  
  &.stat-card-info {
    .stat-icon {
      background: linear-gradient(135deg, #a8edea 0%, #fed6e3 100%);
      color: #409EFF;
    }
    .stat-value {
      color: #409EFF;
    }
  }
}

.chart-row {
  margin-bottom: 20px;
}

.chart-card {
  .card-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    font-weight: 500;
    font-size: 16px;
  }
  
  .chart-container {
    height: 350px;
    width: 100%;
  }
  
  .chart-container-small {
    height: 350px;
    width: 100%;
  }
}

.bottom-row {
  .monitor-card, .quick-action-card, .system-info-card {
    .card-header {
      display: flex;
      justify-content: space-between;
      align-items: center;
      font-weight: 500;
      font-size: 16px;
    }
  }
}

.quick-actions {
  padding: 10px;
  
  .action-item {
    text-align: center;
    padding: 20px 10px;
    cursor: pointer;
    border-radius: 8px;
    transition: all 0.3s;
    margin-bottom: 15px;
    
    &:hover {
      background-color: #f5f7fa;
      transform: translateY(-2px);
    }
    
    .action-icon {
      width: 50px;
      height: 50px;
      margin: 0 auto 10px;
      border-radius: 50%;
      display: flex;
      align-items: center;
      justify-content: center;
      font-size: 24px;
      color: #fff;
      
      &.action-icon-blue {
        background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
      }
      
      &.action-icon-green {
        background: linear-gradient(135deg, #5ee7df 0%, #b490ca 100%);
      }
      
      &.action-icon-purple {
        background: linear-gradient(135deg, #c471f5 0%, #fa71cd 100%);
      }
      
      &.action-icon-orange {
        background: linear-gradient(135deg, #f093fb 0%, #f5576c 100%);
      }
      
      &.action-icon-cyan {
        background: linear-gradient(135deg, #4facfe 0%, #00f2fe 100%);
      }
      
      &.action-icon-red {
        background: linear-gradient(135deg, #fa709a 0%, #fee140 100%);
      }
    }
    
    .action-label {
      font-size: 14px;
      color: #606266;
    }
  }
}

.system-info {
  padding: 10px;
  
  .info-item {
    padding: 12px 0;
    border-bottom: 1px solid #f0f0f0;
    display: flex;
    justify-content: space-between;
    align-items: center;
    
    &:last-child {
      border-bottom: none;
    }
    
    .info-label {
      color: #909399;
      font-size: 14px;
    }
    
    .info-value {
      color: #303133;
      font-size: 14px;
      font-weight: 500;
    }
  }
}

@media (max-width: 768px) {
  .page-header h2 {
    font-size: 22px;
  }
  
  .stat-card {
    .stat-icon {
      width: 50px;
      height: 50px;
      font-size: 24px;
      margin-right: 15px;
    }
    
    .stat-content .stat-value {
      font-size: 24px;
    }
  }
}
</style>

